package com.controller;
import com.entity.Comment;
import com.entity.Goods;
import com.entity.Report;
import com.service.CommentService;
import com.service.ReportService;
import com.util.CodeMsg;
import com.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private ReportService reportService;

    /**
     * 管理员主页
     */
    @GetMapping("")
    public String index() {
        return "admin/index";
    }

    /**
     * 评论管理页面
     */
    @GetMapping("/comments")
    public String comments(Model model) {
        List<Comment> comments = commentService.findAll();
        model.addAttribute("comments", comments);
        return "admin/comments";
    }

    /**
     * 删除评论
     */
    @DeleteMapping(value = "/comments/{id}")
    @ResponseBody
    public Result<Boolean> deleteComment(@PathVariable Long id) {
        boolean success = commentService.deleteById(id);
        return success ? Result.success(true) : Result.error(new CodeMsg(500, "删除评论失败"));
    }


    /**
     * 举报管理页面
     */
    @GetMapping("/reports")
    public String reports(Model model) {
        List<Report> reports = reportService.findAll();
        model.addAttribute("reports", reports);
        return "admin/reports";
    }

    /**
     * 删除举报
     */
    @DeleteMapping(value = "/reports/{id}")
    @ResponseBody
    public Result<Boolean> deleteReport(@PathVariable Long id) {
        boolean success = reportService.deleteById(id);
        return success ? Result.success(true) : Result.error(new CodeMsg(500, "删除举报失败"));
    }

    private String getCurrentUserRole() {

        System.out.println("WARN: Using placeholder getCurrentUserRole() in AdminController");
        return "admin";
    }
    private boolean isAdmin() {
        return "admin".equalsIgnoreCase(getCurrentUserRole());
    }


    /**
     * 获取所有评论 (管理员)
     */
    @GetMapping("/api/comments")
    @ResponseBody
    public ResponseEntity<List<Comment>> getAllComments() {
        if (!isAdmin()) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        List<Comment> comments = commentService.findAll();
        return ResponseEntity.ok(comments);
    }

    /**
     * 获取评论关联的商品
     */
    @GetMapping("/api/comments/{commentId}/goods")
    @ResponseBody
    public Result<Goods> getCommentGoods(@PathVariable Long commentId) {
        if (!isAdmin()) {
            return Result.error(new CodeMsg(403, "无权限访问"));
        }

        Comment comment = commentService.findById(commentId);
        if (comment == null || comment.getGoods() == null) {
            return Result.error(new CodeMsg(404, "评论或商品不存在"));
        }

        return Result.success(comment.getGoods());
    }

    /**
     * 获取所有被举报的评论 (管理员)
     */
    @GetMapping("/api/comments/reported")
    @ResponseBody
    public ResponseEntity<List<Comment>> getReportedComments() {
        if (!isAdmin()) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        List<Comment> comments = commentService.getReportedComments();
        return ResponseEntity.ok(comments);
    }

    /**
     * 删除指定 ID 的评论 (管理员)
     */
    @DeleteMapping("/api/comments/{commentId}")
    @ResponseBody
    public ResponseEntity<Void> deleteCommentApi(@PathVariable Long commentId) {
        if (!isAdmin()) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        boolean deleted = commentService.deleteById(commentId);
        if (deleted) {
            return ResponseEntity.noContent().build(); // Success, no content to return
        } else {
            // Potential reasons: comment not found, or delete failed
            return ResponseEntity.notFound().build();
        }
    }
} 